﻿using MvcPaging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using zichan.DAL;
using zichan.Entity;

namespace zichan.BLL
{
    public class AsseteClassService
    {
        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public static string AddAsseteClass(AsseteClassify model)
        {
            using (ZichanDBContext db = new ZichanDBContext())
            {
                try
                {
                    if (db.AsseteClassifies.Count(o => o.Name == model.Name) > 0)
                        return model.Name + "已存在";
                    if (db.AsseteClassifies.Count(o => o.Identity == model.Identity) > 0)
                        return model.Identity + "已存在";
                    db.AsseteClassifies.Add(model);
                    db.SaveChanges();
                    return string.Empty;
                }
                catch (Exception ex)
                {
                    return "添加失败，请重新添加";
                }
            }
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public static string EditAsseteClass(AsseteClassify model)
        {
            using (ZichanDBContext db = new ZichanDBContext())
            {
                if (db.AsseteClassifies.Count(o => o.Id != model.Id && o.Name == model.Name) > 0)
                    return model.Name + "已存在";
                db.Entry(model).State = System.Data.Entity.EntityState.Modified;
                db.SaveChanges();
                return string.Empty;
            }
        }


        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public static string RemoveAsseteClass(int id)
        {
            using (ZichanDBContext db = new ZichanDBContext())
            {
                AsseteClassify m = db.AsseteClassifies.FirstOrDefault(o => o.Id == id);
                if (m != null)
                {
                    if (db.Assets.Count(o => o.AssetClassId == id) > 0)
                    {
                        return m.Name + "不能删除，已在使用中";
                    }
                    db.AsseteClassifies.Remove(m);
                    db.SaveChanges();
                    ClassLogService.AddLog("资产分类", "删除" + "\"" + m.Name + "\"");
                }
                return string.Empty;
            }
        }

        /// <summary>
        /// 获取单条数据
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public static AsseteClassify GetAsseteClass(int Id)
        {
            using (ZichanDBContext db = new ZichanDBContext())
            {
                return db.AsseteClassifies.FirstOrDefault(o => o.Id == Id);
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="name"></param>
        /// <param name="controller"></param>
        /// <param name="action"></param>
        /// <param name="pageSize"></param>
        /// <param name="pageIndex"></param>
        /// <returns></returns>
        public static IPagedList<AsseteClassify> GetAsseteClassList(string name, string identity, int pageSize, int pageIndex)
        {
            using (ZichanDBContext db = new ZichanDBContext())
            {
                IQueryable<AsseteClassify> w = db.AsseteClassifies;
                if (!string.IsNullOrEmpty(name))
                    w = w.Where(o => o.Name.Contains(name));
                if (!string.IsNullOrEmpty(identity))
                    w = w.Where(o => o.Identity.Contains(identity) || o.SubIdentity.Contains(identity));
                List<AsseteClassify> list = w.OrderBy(o => o.SortId).ThenBy(o=>o.Id).Skip(pageSize * (pageIndex - 1)).Take(pageSize).ToList();
                foreach (AsseteClassify c in list)
                {
                    c.UseCount = db.Assets.Count(o => o.AssetClassId == c.Id);
                }
                return list.OrderBy(o => o.SortId).ThenBy(o=>o.Id).ToPagedList(pageIndex - 1, pageSize, w.Count());
            }
        }
        public static List<AsseteClassify> GetAsseteClassList(string name, string identity)
        {
            using (ZichanDBContext db = new ZichanDBContext())
            {
                IQueryable<AsseteClassify> w = db.AsseteClassifies;
                if (!string.IsNullOrEmpty(name))
                    w = w.Where(o => o.Name.Contains(name));
                if (!string.IsNullOrEmpty(identity))
                    w = w.Where(o => o.Identity.Contains(identity));
                return w.OrderBy(o => o.SortId).ThenBy(o => o.Id).ToList();
            }
        }
        /// <summary>
        /// 批量添加
        /// </summary>
        /// <param name="nameList">输入的名称集合</param>
        /// <param name="existName">重复的名称</param>
        /// <param name="nameListForLog">保存入数据库的名称，用以做日志使用</param>
        /// <returns></returns>
        public static string AddBatchClass(List<string> nameList, out List<string> existName, out List<string> nameListForLog, out List<string> identityListForLog)
        {
            using (ZichanDBContext db = new ZichanDBContext())
            {
                try
                {
                    existName = new List<string>();
                    nameListForLog = new List<string>();
                    identityListForLog = new List<string>();
                    int maxSort = db.AsseteClassifies.Max(o => o.SortId);
                    List<AsseteClassify> classList = new List<AsseteClassify>();
                    foreach (string n in nameList)
                    {
                       string nAndd = Regex.Replace(n,@"[,|，]", ",");
                        List<string> list = nAndd.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries).ToList();
                        string name = list[0];
                        string identity = list[1];
                        if (db.AsseteClassifies.Count(o => o.Name == name) > 0 || classList.Count(o => o.Name == name) > 0)
                        {
                            existName.Add(nAndd);
                            continue;
                        }
                        if (db.AsseteClassifies.Count(o => o.Identity == identity) > 0 || classList.Count(o => o.Identity == identity) > 0)
                        {
                            identityListForLog.Add(list[1]);
                            continue;
                        }
                        nameListForLog.Add(nAndd);
                        AsseteClassify model = new AsseteClassify();
                        maxSort += 1;
                        model.Name = list[0];
                        model.Identity = list[1];
                        model.SortId = maxSort;
                        classList.Add(model);
                    }
                    if (classList.Count() > 0)
                    {
                        db.AsseteClassifies.AddRange(classList);
                        db.SaveChanges();
                    }
                    return string.Empty;
                }
                catch (Exception ex)
                {
                    existName = new List<string>();
                    nameListForLog = new List<string>();
                    identityListForLog = new List<string>();
                    return "无法添加，请重试";
                }
            }
        }
        public static int MaxSort()
        {
            using (ZichanDBContext db = new ZichanDBContext())
            {
                return db.AsseteClassifies.Max(o => o.SortId);
            }
        }
    }
}
